library(pacman)

### Load necessary packages
pacman::p_load( conflicted, data.table,tidyverse, ggplot2, dplyr, readr, tidyr, dplyr, Hmisc, stargazer, stringr, stringi, lfe, xtable, here, readxl,stringdist,mfx) 

rm(list=ls())
conflict_prefer("first", "dplyr")
conflict_prefer("last", "dplyr")
conflict_prefer("here", "here")
conflict_prefer("list", "base")


load(here("..","..","Code","Replication","candidates.Rda"))

specify_decimal <- function(x, k) format(as.numeric(round(x, k), nsmall=k))


###################################
####### MAIN TEXT          ########
###################################

#### Table 3

parties<-cand[,list(num_elections=uniqueN(vrn),
                    perc_elections=uniqueN(vrn),
                    num_candidates=.N,
                    num_rejected=sum(rejected)),by=c("party_factor")]
parties$perc_elections<-round(parties$perc_elections/uniqueN(cand$vrn)*100,1)
parties$perc_rejected<-round(parties$num_rejected/parties$num_candidates*100,1)

totals<-cbind("total",cand[,list(num_elections=uniqueN(vrn),
                                 perc_election="",
                                 num_candidates=.N,
                                 num_rejected=sum(rejected),
                                 perc_rejected="")])
totals$perc_rejected<-round(totals$num_rejected/totals$num_candidates*100,1)

names(totals)<-names(parties)
parties<-rbind(parties,totals)

parties$party_factor<-c("Independent","Communist Party","United Russia","LDPR","Smaller Parties","Just Russia","Total")

parties<-as.data.frame(parties)
names(parties)<-c("Party","Num. Elections"," (% of Total)","Num. Candidates","Num. Rejected", "Rejected (%)")



#### Table 4

SummaryTables <- data.frame(name= numeric(0),notrejected= numeric(0),rejected= numeric(0))

SummaryTables[1 ,] <- c("(1) Number of Candidates",prettyNum(length(unique(cand$vibid[cand$rejected==0])),big.mark=","),prettyNum(length(unique(cand$vibid[cand$rejected==1])),big.mark=","))

SummaryTables[2 ,] <- c("(2) Age (mean)",specify_decimal(mean(cand$age[cand$rejected==0]),1),specify_decimal(mean(cand$age[cand$rejected==1]),1))

SummaryTables[3 ,] <- c("(3) Female (%)",specify_decimal(mean(cand$female[cand$rejected==0])*100,1),specify_decimal(mean(cand$female[cand$rejected==1])*100,1))

SummaryTables[4 ,] <- c("(4) College Education (%)",specify_decimal(mean(cand$edu_college[cand$rejected==0],na.rm=TRUE)*100,1),specify_decimal(mean(cand$edu_college[cand$rejected==1],na.rm=TRUE)*100,1))

SummaryTables[5 ,] <- c("(5) Businessperson (%)",specify_decimal(mean(cand$businessperson[cand$rejected==0])*100,1),specify_decimal(mean(cand$businessperson[cand$rejected==1])*100,1))

SummaryTables[6 ,] <- c("(6) Num. Previous Campaigns (mean)",specify_decimal(mean(cand$num_real_campaigns[cand$rejected==0]),1),specify_decimal(mean(cand$num_real_campaigns[cand$rejected==1]),1))

SummaryTables[7 ,] <- c("(7) Num. Previous Wins (mean)",specify_decimal(mean(cand$num_wins[cand$rejected==0]),1),specify_decimal(mean(cand$num_wins[cand$rejected==1]),1))

SummaryTables[8 ,] <- c("(8) Incumbent (%)",specify_decimal(mean(cand$incumbent[cand$rejected==0])*100,1),specify_decimal(mean(cand$incumbent[cand$rejected==1])*100,1))

colnames(SummaryTables) <- c(" ","Registered Candidates","Rejected Candidates")



####### Table 5 

cand$party_factor<-relevel(factor(cand$party_factor),"ur")

est1<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type)| 0 | vrn, data=cand)

est2<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est3<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

cand$party_factor<-relevel(factor(cand$party_factor),"ldpr")

est4<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est5<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est6<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))


##### Figure 2 

rejecteds_year<-cand[,list(rejected=mean(rejected)*100,N=.N),by=c("electionyear")]

rejecteds_year$electionyear<-as.character(rejecteds_year$electionyear)
rejecteds_year$label <-paste0(specify_decimal(rejecteds_year$rejected,1),"%",sep="")
ggplot(rejecteds_year, aes(x=electionyear,y=rejected))+geom_bar(stat = "identity")+xlab("Year")+ylab("Rejection Rate (%)")+theme(axis.text=element_text(size=18),axis.title=element_text(size=18)) + guides(fill=guide_legend(title=" "))+ geom_text(aes(label=label), position=position_dodge(width=0.9), vjust=-0.4,size=4)+ylim(0,17.5)



###################################
####### APPENDIX           ########
###################################

#### Tqble A1

elections_summary<-cand[,list(log_age,female,edu_college,businessperson,num_real_campaigns,num_wins,num_rejected,incumbent,openseat,ind,kprf,ldpr,sr,ur,other,party_factor,log_pop,log_territory,totalexpend_log)]

electionstable<-stargazer(elections_summary,covariate.labels=c(
  "Age (log)","Female","College Education","Businessperson","Num. Previous Campaigns","Num. Previous Wins","Num. Previous Rejections","Candidate was Incumbent","Open Seat","Independent","Communist Party","LDPR","Just Russia","United Russia","Smaller Parties","Municipality - Population (log)","Municipality - Territory (log)","Municipality - Expenditures (log)"),header=FALSE,digits=3)


#### Table B1

cand$number_of_wins<-cand$num_wins
cand$party_factor<-relevel(factor(cand$party_factor),"ur")

est1<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est2<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est3<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

cand$party_factor<-relevel(factor(cand$party_factor),"ldpr")

est4<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est5<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est6<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))


#### Table B2

cand$number_of_wins<-cand$num_wins
cand$party_factor<-relevel(factor(cand$party_factor),"ur")

est1<-felm(rejected ~ log_age + female + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est2<-felm(rejected ~ log_age + female + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est3<-felm(rejected ~ log_age + female + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

cand$party_factor<-relevel(factor(cand$party_factor),"ldpr")

est4<-felm(rejected ~ log_age + female+ businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est5<-felm(rejected ~ log_age + female + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est6<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))


#### Table B3

cand$party_factor<-relevel(factor(cand$party_factor),"ur")

est1<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + party_factor + log_pop +log_territory+totalexpend_log| factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est2<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat  + party_factor+ log_pop +log_territory+totalexpend_log | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est3<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins + log_pop +log_territory+totalexpend_log | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

cand$party_factor<-relevel(factor(cand$party_factor),"ldpr")

est4<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + party_factor + log_pop +log_territory+totalexpend_log| factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est5<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat  + party_factor + log_pop +log_territory+totalexpend_log| factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est6<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins + log_pop +log_territory+totalexpend_log| factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))


#### Table B4



est1<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est2<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

cand$number_of_wins=cand$num_wins_municipal

est3<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins_municipal + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est4<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins_municipal + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

cand$number_of_wins=cand$num_wins_city

est5<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins_city + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est6<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins_city + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

cand$number_of_wins=cand$num_wins_regional

est7<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins_regional + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est8<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins_regional + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))


#### Table B5



### Including UR in the Regressions
cand$number_of_wins<-cand$num_wins
cand$party_factor<-relevel(factor(cand$party_factor),"ur")

est1<-felm(rejected_simple ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type)| 0 | vrn, data=cand)

est2<-felm(rejected_simple ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est3<-felm(rejected_simple ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

cand$party_factor<-relevel(factor(cand$party_factor),"ldpr")

est4<-felm(rejected_simple ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est5<-felm(rejected_simple ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est6<-felm(rejected_simple ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + openseat + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))


#### Table B6

cand$party_factor<-relevel(factor(cand$party_factor),"ur")

est1<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type)| 0 | vrn, data=cand)

est2<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent  + previousshare3 + party_factor  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

est3<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + previousshare3 + party_factor*number_of_wins  | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=cand)

cand$party_factor<-relevel(factor(cand$party_factor),"ldpr")

est4<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est5<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + previousshare3  + party_factor | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))

est6<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_wins + incumbent + previousshare3 + party_factor*number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ur==0))


#### Table B7


cand$regionnum_factor<-factor(cand$regionnum)
cand$electionyear_factor<-factor(cand$electionyear)
cand$unit_type_factor<-factor(cand$unit_type)
cand$number_of_wins<-cand$num_wins
cand$party_factor<-relevel(factor(cand$party_factor),"ur")


est1_f<-as.formula("rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + party_factor + regionnum_factor + electionyear_factor + unit_type_factor")
est1<-glm(est1_f,data=cand, family=binomial(link="logit"))
est1m<-logitmfx(est1_f, cand, atmean = TRUE, robust = TRUE, clustervar1 = "vrn")

est2_f<-as.formula("rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + openseat + party_factor + regionnum_factor + electionyear_factor + unit_type_factor")
est2<-glm(est2_f,data=cand, family=binomial(link="logit"))
est2m<-logitmfx(est2_f, cand, atmean = TRUE, robust = TRUE, clustervar1 = "vrn")

est3_f<-as.formula("rejected ~ log_age + female + edu_college + num_real_campaigns + num_wins + incumbent + openseat + party_factor*number_of_wins + regionnum_factor + electionyear_factor + unit_type_factor")
est3<-glm(est3_f,data=cand, family=binomial(link="logit"))
est3m<-logitmfx(est3_f, cand, atmean = TRUE, robust = TRUE, clustervar1 = "vrn")

cand$party_factor<-relevel(factor(cand$party_factor),"ldpr")

est4_f<-as.formula("rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + party_factor + regionnum_factor + electionyear_factor + unit_type_factor")
est4<-glm(est4_f,data=subset(cand, ur==0), family=binomial(link="logit"))
est4m<-logitmfx(est4_f, subset(cand, ur==0), atmean = TRUE, robust = TRUE, clustervar1 = "vrn")

est5_f<-as.formula("rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + openseat + party_factor + regionnum_factor + electionyear_factor + unit_type_factor")
est5<-glm(est5_f,data=subset(cand, ur==0), family=binomial(link="logit"))
est5m<-logitmfx(est5_f, subset(cand, ur==0), atmean = TRUE, robust = TRUE, clustervar1 = "vrn")

est6_f<-as.formula("rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_wins + incumbent + openseat + party_factor*number_of_wins + regionnum_factor + electionyear_factor + unit_type_factor")
est6<-glm(est6_f,data=subset(cand, ur==0), family=binomial(link="logit"))
est6m<-logitmfx(est6_f, subset(cand, ur==0), atmean = TRUE, robust = TRUE, clustervar1 = "vrn")

#### Table B8


SummaryTables <- data.frame(title=numeric(0),
                            ur= numeric(0),
                            kprf= numeric(0),
                            ldpr=numeric(0),
                            sr=numeric(0),
                            ind=numeric(0),
                            topind=numeric(0),
                            indrej=numeric(0)
                            
)

SummaryTables[1 ,] <- c("(1) Total Num.", 
                        uniqueN(cand$vibid[cand$ur==1]),
                        uniqueN(cand$vibid[cand$kprf==1]),
                        uniqueN(cand$vibid[cand$ldpr==1]),
                        uniqueN(cand$vibid[cand$sr==1]),
                        uniqueN(cand$vibid[cand$ind==1]),
                        uniqueN(cand$vibid[cand$topind==1]),
                        uniqueN(cand$vibid[cand$indrej==1])
)


SummaryTables[2 ,] <- c("(2) Mean Age", 
                        mean(cand$age[cand$ur==1],na.rm=TRUE),
                        mean(cand$age[cand$kprf==1],na.rm=TRUE),
                        mean(cand$age[cand$ldpr==1],na.rm=TRUE),
                        mean(cand$age[cand$sr==1],na.rm=TRUE),
                        mean(cand$age[cand$ind==1],na.rm=TRUE),
                        mean(cand$age[cand$topind==1],na.rm=TRUE),
                        mean(cand$age[cand$indrej==1],na.rm=TRUE))



SummaryTables[3 ,] <- c("(3) Female (%)", 
                        mean(cand$female[cand$ur==1],na.rm=TRUE)*100,
                        mean(cand$female[cand$kprf==1],na.rm=TRUE)*100,
                        mean(cand$female[cand$ldpr==1],na.rm=TRUE)*100,
                        mean(cand$female[cand$sr==1],na.rm=TRUE)*100,
                        mean(cand$female[cand$ind==1],na.rm=TRUE)*100,
                        mean(cand$female[cand$topind==1],na.rm=TRUE)*100,
                        mean(cand$female[cand$indrej==1],na.rm=TRUE)*100)


SummaryTables[4 ,] <- c("(4) College Education (%)", 
                        mean(cand$edu_college[cand$ur==1],na.rm=TRUE)*100,
                        mean(cand$edu_college[cand$kprf==1],na.rm=TRUE)*100,
                        mean(cand$edu_college[cand$ldpr==1],na.rm=TRUE)*100,
                        mean(cand$edu_college[cand$sr==1],na.rm=TRUE)*100,
                        mean(cand$edu_college[cand$ind==1],na.rm=TRUE)*100,
                        mean(cand$edu_college[cand$topind==1],na.rm=TRUE)*100,
                        
                        mean(cand$edu_college[cand$indrej==1],na.rm=TRUE)*100)

SummaryTables[5 ,] <- c("(5) Businessperson (%)", 
                        mean(cand$businessperson[cand$ur==1],na.rm=TRUE)*100,
                        mean(cand$businessperson[cand$kprf==1],na.rm=TRUE)*100,
                        mean(cand$businessperson[cand$ldpr==1],na.rm=TRUE)*100,
                        mean(cand$businessperson[cand$sr==1],na.rm=TRUE)*100,
                        mean(cand$businessperson[cand$ind==1],na.rm=TRUE)*100,
                        mean(cand$businessperson[cand$topind==1],na.rm=TRUE)*100,
                        
                        mean(cand$businessperson[cand$indrej==1],na.rm=TRUE)*100)

SummaryTables[6 ,] <- c("(6) Num. Previous Campaigns", 
                        mean(cand$num_real_campaigns[cand$ur==1],na.rm=TRUE),
                        mean(cand$num_real_campaigns[cand$kprf==1],na.rm=TRUE),
                        mean(cand$num_real_campaigns[cand$ldpr==1],na.rm=TRUE),
                        mean(cand$num_real_campaigns[cand$sr==1],na.rm=TRUE),
                        mean(cand$num_real_campaigns[cand$ind==1],na.rm=TRUE),
                        mean(cand$num_real_campaigns[cand$topind==1],na.rm=TRUE),
                        
                        mean(cand$num_real_campaigns[cand$indrej==1],na.rm=TRUE))

SummaryTables[7 ,] <- c("(7) Num. Previous Wins", 
                        mean(cand$num_wins[cand$ur==1],na.rm=TRUE),
                        mean(cand$num_wins[cand$kprf==1],na.rm=TRUE),
                        mean(cand$num_wins[cand$ldpr==1],na.rm=TRUE),
                        mean(cand$num_wins[cand$sr==1],na.rm=TRUE),
                        mean(cand$num_wins[cand$ind==1],na.rm=TRUE),
                        mean(cand$num_wins[cand$topind==1],na.rm=TRUE),
                        
                        mean(cand$num_wins[cand$indrej==1],na.rm=TRUE))

SummaryTables[8 ,] <- c("(8) Vote Share", 
                        mean(cand$percent[cand$ur==1],na.rm=TRUE)*100,
                        mean(cand$percent[cand$kprf==1],na.rm=TRUE)*100,
                        mean(cand$percent[cand$ldpr==1],na.rm=TRUE)*100,
                        mean(cand$percent[cand$sr==1],na.rm=TRUE)*100,
                        mean(cand$percent[cand$ind==1],na.rm=TRUE)*100,
                        mean(cand$percent[cand$topind==1],na.rm=TRUE)*100,
                        
                        NA)

SummaryTables[9 ,] <- c("(9) Elections Won (%)", 
                        mean(cand$wonofficial[cand$ur==1],na.rm=TRUE)*100,
                        mean(cand$wonofficial[cand$kprf==1],na.rm=TRUE)*100,
                        mean(cand$wonofficial[cand$ldpr==1],na.rm=TRUE)*100,
                        mean(cand$wonofficial[cand$sr==1],na.rm=TRUE)*100,
                        mean(cand$wonofficial[cand$ind==1],na.rm=TRUE)*100,
                        mean(cand$wonofficial[cand$topind==1],na.rm=TRUE)*100,
                        
                        NA)

#### Table B9

cand$number_of_wins<-cand$num_wins
cand$party_factor<-relevel(factor(cand$party_factor),"ur")

est1<-felm(rejected ~ log_age + female + edu_college+ businessperson + num_real_campaigns + num_rejected + num_wins + incumbent | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ind==1))

est2<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ind==1))

est3<-felm(rejected ~ log_age + female + edu_college + businessperson + num_real_campaigns + num_rejected + num_wins + incumbent + openseat + number_of_wins | factor(regionnum) + factor(electionyear) + factor(unit_type) | 0 | vrn, data=subset(cand, ind==1))




#### Table C1


vrns<-cand[cand$numvictors<=1][,list(rejected=sum(rejected),
                                     rejected_ind_strong=sum(rejected[ind==1 & number_of_wins>0]),
                                     total_ind_strong=uniqueN(vibid[ind==1 & number_of_wins>0]),
                                     
                                     margin=max(margin,na.rm=TRUE),
                                     ur_share=max(percent[ur==1],na.rm=TRUE),
                                     ur_winner=ur[wonofficial==1],
                                     num_candidates_first=uniqueN(vibid),
                                     numvictors=max(numvictors,na.rm=TRUE),
                                     num_candidates_perc=uniqueN(vibid[is.na(percent)==FALSE]),
                                     electiontotalvotes=min(electiontotalvotes,na.rm=TRUE),
                                     voterlist=min(as.numeric(voterlist),na.rm=TRUE),
                                     totalexpend_log=min(totalexpend_log,na.rm=TRUE),
                                     log_pop=min(log_pop,na.rm=TRUE),
                                     log_territory=min(log_territory,na.rm=TRUE)
),by=c("vrn","regionnum","electionyear","unit_type")]
vrns<-subset(vrns, num_candidates_perc>0)
vrns$ur_share[vrns$ur_share=="-Inf"]<-NA
vrns$log_territory[vrns$log_territory=="Inf"]<-NA

vrns$turnout<-vrns$electiontotalvotes/as.numeric(vrns$voterlist)
vrns$log_cand<-log(vrns$num_candidates_perc)

vrns$totalexpend_log[vrns$totalexpend_log=="Inf"]<-NA
vrns$log_territory[vrns$log_territory=="Inf"]<-NA
vrns$log_votes<-log(vrns$voterlist)

est1<-felm(ur_winner~rejected_ind_strong + total_ind_strong + log_votes + log_cand | factor(regionnum) + factor(electionyear) + factor(unit_type)|0|regionnum,data=subset(vrns, is.na(ur_share)==FALSE))

est2<-felm(ur_winner~rejected_ind_strong + total_ind_strong + log_votes + log_cand + totalexpend_log + log_territory | factor(regionnum) + factor(electionyear) + factor(unit_type)|0|regionnum,data=subset(vrns, is.na(ur_share)==FALSE))

est3<-felm(ur_share~rejected_ind_strong + total_ind_strong + log_votes + log_cand | factor(regionnum) + factor(electionyear) + factor(unit_type)|0|regionnum,data=vrns)

est4<-felm(ur_share~rejected_ind_strong + total_ind_strong + log_votes + log_cand + totalexpend_log + log_territory | factor(regionnum) + factor(electionyear) + factor(unit_type)|0|regionnum,data=vrns)

est5<-felm(turnout~rejected_ind_strong + total_ind_strong + log_votes + log_cand | factor(regionnum) + factor(electionyear) + factor(unit_type)|0|regionnum,data=vrns)

est6<-felm(turnout~rejected_ind_strong + total_ind_strong + log_votes + log_cand + totalexpend_log + log_territory | factor(regionnum) + factor(electionyear) + factor(unit_type)|0|regionnum,data=vrns)

